<!--
  * 公共应用表
  *
  * @Author:    zhoujk
  * @Date:      2025-01-26 06:40:44
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="应用名称" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.appName" placeholder="应用名称" allow-clear />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <!--<div class="smart-table-operate-block">-->
      <!--  <a-button @click="showForm" type="primary" size="small">-->
      <!--    <template #icon>-->
      <!--      <PlusOutlined />-->
      <!--    </template>-->
      <!--    新建-->
      <!--  </a-button>-->
      <!--</div>-->
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <!-- <a-button @click="showForm" type="primary" class="mr-2" size="small" v-if="queryForm.agentType == 2">
          <template #icon>
            <PlusOutlined />
          </template>
          新建
        </a-button> -->
        <!-- <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.OPERATION.MERCHANT" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <!--<a-button @click="showForm(record)" type="link">编辑</a-button>-->
            <a-button @click="showDetail(record)" type="link">详情</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>

  <appAuthList v-model:open="appAuthOptions.open" :row-data="appAuthOptions.rowData" />
</template>
<script setup>
  import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
  import { message } from 'ant-design-vue'
  import { applicationsPage } from '/@/api/business/b-operation/merchant-app/common-app-api'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import appAuthList from './app-auth-list.vue'
  import { smartSentry } from '/@/lib/smart-sentry'
  import { isEmpty } from 'lodash'
  import { selectDictLabel } from '/@/utils/common'
  import {
    AUTH_APP, //授权产品，产品列表
    DEV_TYPE, ///开发类型
  } from '/@/constants/dict'

  const { proxy } = getCurrentInstance()
  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      customRender: ({ index, record }) => {
        return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
      },
    },
    {
      title: '应用名称/ID',
      dataIndex: 'appName',
      customRender: ({ text, record }) => {
        return `${text}/${record.id}`
      },
      ellipsis: true,
    },
    {
      title: '服务商账号',
      dataIndex: 'serviceAccount',
      ellipsis: true,
    },
    {
      title: '开发状态',
      dataIndex: 'status',
      customRender: ({ text }) => {
        return selectDictLabel(DEV_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 180,
    },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    appName: undefined, //应用名称
    pageNo: 1,
    pageSize: 10,
  }
  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await applicationsPage(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  onMounted(queryData)
  // ---------------------------- 详情 ----------------------------.
  const appAuthOptions = ref({ open: false, rowData: {} })
  function showDetail(data) {
    appAuthOptions.value.open = true
    appAuthOptions.value.rowData = data
  }
</script>
